import axios from "axios";
import { BASE_URL } from "../config/http.js";
// 获取 token
function getToken() {
  return sessionStorage.getItem("token") || "";
}

// 获取所有用户
export function getAllUsers(requesterId) {
  return axios
    .get(`${BASE_URL}/api/user?requesterId=${requesterId}`, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 获取单个用户信息
export function getUserById(userId) {
  return axios
    .get(`${BASE_URL}/api/user/${userId}`, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 添加用户
export function addUserApi(data) {
  return axios
    .post(`${BASE_URL}/api/user/register`, data, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 更新用户
export function updateUserApi(id, data) {
  return axios
    .put(`${BASE_URL}/api/user/${id}`, data, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

export function deleteUserApi(id, deleterId) {
  return axios
    .delete(`${BASE_URL}/api/user/${id}?deleterId=${deleterId}`, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 获取所有主页审批申请
export function getAllHomeApprovals() {
  return axios
    .get(`${BASE_URL}/api/homeApproval/`, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 更新审批申请
export function updateHomeApproval(id, data) {
  return axios
    .put(`${BASE_URL}/api/homeApproval/${id}`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 删除审批申请
export function deleteHomeApproval(id, data) {
  return axios
    .delete(`${BASE_URL}/api/homeApproval/${id}`, {
      data,
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 添加主页数据
export function addHomeData(data) {
  return axios
    .post(`${BASE_URL}/api/homeData/`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 获取所有主页数据
export function getAllHomeData() {
  return axios
    .get(`${BASE_URL}/api/homeData/`, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 更新主页数据
export function updateHomeData(id, data) {
  return axios
    .put(`${BASE_URL}/api/homeData/${id}`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 删除主页数据
export function deleteHomeData(id, data) {
  return axios
    .delete(`${BASE_URL}/api/homeData/${id}`, {
      data,
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 获取所有书本数据（按Layers降序排序）
export function getAllBookData() {
  return axios.get(`${BASE_URL}/api/bookData/`).then((res) => res.data);
}

// 更新书本数据
export function updateBookData(id, data) {
  return axios
    .put(`${BASE_URL}/api/bookData/${id}`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 删除书本数据
export function deleteBookData(id, operatorId) {
  return axios
    .delete(`${BASE_URL}/api/bookData/${id}`, {
      data: { operatorId },
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 获取所有角色审批申请
export function getAllCharacterApprovals() {
  return axios
    .get(`${BASE_URL}/api/approveCharacters/`, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 管理员/社长审批角色（修改审批状态和备注）
export function approveCharacter(id, data) {
  return axios
    .put(`${BASE_URL}/api/approveCharacters/${id}`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 删除角色（只有社长或管理员可以删除）
export function deleteCharacter(id, operatorId) {
  return axios
    .delete(`${BASE_URL}/api/approveCharacters/${id}`, {
      data: { operatorId },
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 添加角色数据（正式表）
export function addCharacterData(data) {
  return axios
    .post(`${BASE_URL}/api/charactersData`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 查询全部角色数据
export function getAllCharactersData() {
  return axios
    .get(`${BASE_URL}/api/charactersData`, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 更新角色数据
export function updateCharacterData(id, data) {
  return axios
    .put(`${BASE_URL}/api/charactersData/${id}`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 删除角色数据
export function deleteCharacterData(id, operatorId) {
  return axios
    .delete(`${BASE_URL}/api/charactersData/${id}`, {
      data: { operatorId },
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 查询 PageAssets
export function getPageAssets() {
  return axios
    .get(`${BASE_URL}/api/pageAssets`, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 修改 PageAssets
export function updatePageAssets(data) {
  return axios
    .put(`${BASE_URL}/api/pageAssets`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 查询全部视频数据
export function getAllVideoData() {
  return axios
    .get(`${BASE_URL}/api/videoData`, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 创建视频数据
export function addVideoData(data) {
  return axios
    .post(`${BASE_URL}/api/videoData`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 更新视频数据
export function updateVideoData(id, data) {
  return axios
    .put(`${BASE_URL}/api/videoData/${id}`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 删除视频数据
export function deleteVideoData(id, operatorId) {
  return axios
    .delete(`${BASE_URL}/api/videoData/${id}`, {
      data: { operatorId },
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 查询全部公告
export function getAllAnnouncementData() {
  return axios
    .get(`${BASE_URL}/api/announcementData`, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 创建公告
export function addAnnouncementData(data) {
  return axios
    .post(`${BASE_URL}/api/announcementData`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 更新公告
export function updateAnnouncementData(id, data) {
  return axios
    .put(`${BASE_URL}/api/announcementData/${id}`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 删除公告
export function deleteAnnouncementData(id, operatorId) {
  return axios
    .delete(`${BASE_URL}/api/announcementData/${id}`, {
      data: { operatorId },
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}
